package com.example.dao.implDao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.ClassesDao;
import com.example.mapper.ClassesMapper;
import com.example.pojo.Classes;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

@Repository
public class ClassesDaoImpl implements ClassesDao {
    private final ClassesMapper classesMapper;

    public ClassesDaoImpl(ClassesMapper classesMapper) {
        this.classesMapper = classesMapper;
    }


    @Override
    public List<Classes> getAllClasses() {
        return classesMapper.selectList(null);
    }

    @Override
    public IPage<Classes> getClassesPage(int currentPage, int limit) {
         Page<Classes> page = new Page<>(currentPage,limit);
         return classesMapper.selectPage(page,null);
    }

    @Override
    public Classes getClassInfoById(Long classId) {
        return classesMapper.selectById(classId);
    }

    @Override
    public String addOrUpdateClasses(Classes classes) {
        String result = "";
        if(Objects.isNull(classes.getClassId())) {
            classesMapper.insert(classes);
            result="添加成功";
        }else{
            classesMapper.updateById(classes);
            result="已分配班级";
        }
        return result;
    }

    @Override
    public String deleteClasses(Long classesId) {
        String result = "删除失败，请稍后再试";
        int i = classesMapper.deleteById(classesId);
        if(i>0){
            result="删除成功";
        }
        return result;
    }
}
